package t4j.io;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.regex.Pattern;

public class DirList {

	public static void main(String[] args) {
		File path = new File(".");

		String[] list;

		if (args.length == 0)
			list = path.list();
		else
			list = path.list(new DirFilter("^\\.\\w+"));//回调，使用过滤器匹配符合正则表达式的文件名

		Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);//排序

		for (String dirItem : list)
			System.out.println(dirItem);
	}
}

//实现FilenameFilter
class DirFilter implements FilenameFilter {
	private Pattern pattern;

	public DirFilter(String regex) {
		pattern = Pattern.compile(regex);
	}

	@Override
	public boolean accept(File dir, String name) {

		return pattern.matcher(name).matches();// 整串匹配：当整个字符串匹配模式时才返回 true
	}
}